1
|
|
|
import { |
2
|
|
|
Controller, |
3
|
|
|
Inject, |
4
|
|
|
BadRequestException, |
5
|
|
|
UseGuards, |
6
|
|
|
Param, |
7
|
|
|
Body, |
8
|
|
|
Post, |
9
|
|
|
Res |
10
|
|
|
} from '@nestjs/common'; |
11
|
|
|
import { Response } from 'express'; |
12
|
|
|
import { IdDTO } from 'src/Infrastructure/Common/DTO/IdDTO'; |
13
|
|
|
import { User } from 'src/Domain/HumanResource/User/User.entity'; |
14
|
|
|
import { LoggedUser } from '../../User/Decorator/LoggedUser'; |
15
|
|
|
import { ICommandBus } from 'src/Application/ICommandBus'; |
16
|
|
|
import { ModerationAction, ModerationDTO } from '../DTO/ModerationDTO'; |
17
|
|
|
import { AcceptLeaveRequestCommand } from 'src/Application/HumanResource/Leave/Command/AcceptLeaveRequestCommand'; |
18
|
|
|
import { IsAuthenticatedGuard } from '../../User/Security/IsAuthenticatedGuard'; |
19
|
|
|
import { RefuseLeaveRequestCommand } from 'src/Application/HumanResource/Leave/Command/RefuseLeaveRequestCommand'; |
20
|
|
|
import { RouteNameResolver } from 'src/Infrastructure/Common/ExtendedRouting/RouteNameResolver'; |
21
|
|
|
import { WithName } from 'src/Infrastructure/Common/ExtendedRouting/WithName'; |
22
|
|
|
|
23
|
|
|
@Controller('app/people/leave-requests/moderation') |
24
|
|
|
@UseGuards(IsAuthenticatedGuard) |
25
|
|
|
export class ModerateLeaveRequestController { |
26
|
|
|
constructor( |
27
|
|
|
@Inject('ICommandBus') |
28
|
|
|
private readonly commandBus: ICommandBus, |
29
|
|
|
private readonly resolver: RouteNameResolver |
30
|
|
|
) {} |
31
|
|
|
|
32
|
|
|
@Post(':id') |
33
|
|
|
@WithName('people_leave_requests_moderation') |
34
|
|
|
public async post( |
35
|
|
|
@Param() { id }: IdDTO, |
36
|
|
|
@Body() { comment, action }: ModerationDTO, |
37
|
|
|
@LoggedUser() user: User, |
38
|
|
|
@Res() res: Response |
39
|
|
|
) { |
40
|
|
|
const command = |
41
|
|
|
action === ModerationAction.ACCEPT |
42
|
|
|
? new AcceptLeaveRequestCommand(user, id, comment) |
43
|
|
|
: new RefuseLeaveRequestCommand(user, id, comment); |
44
|
|
|
|
45
|
|
|
try { |
46
|
|
|
await this.commandBus.execute(command); |
47
|
|
|
|
48
|
|
|
res.redirect(303, this.resolver.resolve('people_leave_requests_list')); |
49
|
|
|
} catch (e) { |
50
|
|
|
throw new BadRequestException(e.message); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|